<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2018/4/12
 * Time: 18:13
 */

namespace App\Http\Base;


use App\Http\Utils\JUtil;
use App\Http\Utils\PageUtil;
use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model
{
    /**
     * 分页
     * @param $where
     * @param string $listName
     * @return array
     */
    public function page($where,  $listName   = 'list')
    {
        $page = PageUtil::P();
        $size = PageUtil::PS();

        $startIndex = ($page - 1) * $size;                      // 开始下标
        $result     = $this->where($where)->offset($startIndex)->limit($size)->get();
        $count      = $this->getCount($where);

        $totalPage  = ceil($count / $size);

        $returnData = array(
            $listName   => $result,                     // 列表数据
            'page'      => intval($page),               // 当前页
            'pageSize'  => intval($size),               // 页大小
            'totalPage' => intval($totalPage)           // 总页数
        );
        return JUtil::P($returnData);
    }

    /**
     * 获取页码
     * @param $where
     * @return int
     */
    public function getCount($where)
    {
        return $this->where($where)->count();
    }



}